package com.guonima.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author : guonima
 * @create : 2017-10-30-21:41
 */
@Service
public class TestService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 断路器控制，当出现连接超时等错误会调用testFallback方法
     * @param name
     * @return
     */
    @HystrixCommand(fallbackMethod = "testFallback")
    public String test(String name){
        return restTemplate.getForObject("http://eureka-client/test?name=" + name, String.class);
    }

    /**
     * fallback方法的参数返回值要和需要断路的
     * @param name
     * @return
     */
    public String testFallback(String name){

        return "hi, " + name + " error, sorry.";
    }
}
